﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace parametric_resonance
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int s2i(string s)
        {
            if (s == "")
            {
                return 0;
            }
            return Convert.ToInt32(s);
        }

        private double s2d(string s)
        {
            if (s == "")
            {
                return 0;
            }
            s = s.Replace('.', ',');
            return Convert.ToDouble(s);
        }
        //private long d2i(double d)
        //{
        //    return Convert.ToInt64(d);
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            int N = s2i(textBox4.Text);       // кол-во итераций
            pendulum p1 = new pendulum(s2d(textBox1.Text), s2d(textBox2.Text), s2d(textBox3.Text), s2d(textBox5.Text), s2d(textBox7.Text), N);
            p1.x1 = s2d(textBox9.Text);

            Graphics g = pictureBox1.CreateGraphics();
            g.FillRectangle(Brushes.White, 0, 0, 300, 300);
            g.DrawLine(Pens.Black, 0, 150, 300, 150);
            g.DrawLine(Pens.Black, 150, 0, 150, 300);
            Graphics g2 = pictureBox2.CreateGraphics();
            g2.FillRectangle(Brushes.White, 0, 0, 300, 300);
            g2.DrawLine(Pens.Black, 0, 150, 300, 150);
            g2.DrawLine(Pens.Black, 150, 0, 150, 300);
            Graphics g3 = pictureBox3.CreateGraphics();
            g3.FillRectangle(Brushes.White, 0, 0, 300, 300);
            g3.DrawLine(Pens.Black, 0, 150, 300, 150);
            g3.DrawLine(Pens.Black, 150, 0, 150, 300);


            // график
            double scalex = s2d(textBox8.Text);  // масштаб по x
            double scalet = s2d(textBox6.Text);  // масштаб по x

            // фазовый портрет
            double scaledx = s2d(textBox10.Text);  // масштаб по dx
            double scaledp = s2d(textBox11.Text);  // масштаб по dp


            for (int n = 2; n <= N; n++)
            {
                p1.step(n, scalex, scalet, scaledp, scaledx);
            }
            g.DrawCurve(Pens.Black, p1.points, 0, p1.num_points, 0);
            g2.DrawCurve(Pens.Black, p1.pointsPh, 0, p1.num_pointsPh, 0);


            for (int n = 0; n <= p1.num_pointsPh; n++)
            {
                g3.DrawEllipse(Pens.Black, p1.pointsPh[n].X, p1.pointsPh[n].Y, 1, 1);
            }
             pictureBox1.Update();
            
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            double w0 = s2d(textBox1.Text);    // невозмущенная частота собственных колебаний
            double gamma = s2d(textBox3.Text); // 

            double h;                         // величина возмущения
            h = 4 * gamma / (w0 * w0 - gamma * gamma);
            textBox2.Text = h.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double w0 = s2d(textBox1.Text);    // невозмущенная частота собственных колебаний
            double gamma;

            gamma = 2 * w0;
            textBox3.Text = gamma.ToString();
        }
    }
}
